package me.winds.infrared.utils;

import java.io.UnsupportedEncodingException;
import java.util.regex.Pattern;

/**
 * Author by Winds on 2016/10/18.
 * Email heardown@163.com.
 */
public class StringUtils {

    /**
     * 字符序列转换为16进制字符串
     * @param src
     * @return
     */
    public static String bytes2HexString(byte[] src) {
        StringBuilder builder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return null;
        }
        char[] buffer = new char[2];
        for (int i = 0; i < src.length; i++) {
            buffer[0] = Character.forDigit((src[i] >>> 4) & 0x0F, 16);
            buffer[1] = Character.forDigit(src[i] & 0x0F, 16);
            builder.append(buffer);
        }
        return builder.toString();
    }

    /**
     * 十六进制字节数组转十进制数
     *
     * @param src
     * @return
     */
    public static long hexByte2Dec(byte[] src) {
        StringBuilder builder = new StringBuilder();
        String hex = bytes2HexString(src);
        for (int i = 0; i < hex.length() / 2; i++) {
            String str = hex.substring(hex.length() - 2 * (i + 1), hex.length() - 2 * i);
            builder.append(str);
        }
        return Long.parseLong(builder.toString(), 16);
    }


    /**
     * hexStr to decimal
     *
     * @param hex
     * @return
     */
    public static long hexStr2Decimal(String hex) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < hex.length() / 2; i++) {
            String str = hex.substring(hex.length() - 2 * (i + 1), hex.length() - 2 * i);
            System.out.println(str);
            builder.append(str);
        }
        return Long.parseLong(builder.toString(), 16);
    }


    /**
     * 把十进制数字转换成足位的十六进制字符串 -->未补全空位
     *
     * @param num
     * @return
     */
    private static String getHexNumString(int num) {
        return num < 16 ? ("0" + Integer.toHexString(num)) : Integer.toHexString(num);
    }

    /**
     * 把十进制数字转换成足位的十六进制字符串 -->补全空位
     *
     * @param num
     * @return
     */
    public static String getHexNum(int num) {
        String hex = Integer.toHexString(num);
        if (hex.length() % 2 != 0) {
            return "0" + hex;
        }

        return hex;
    }


    /**
     * 字符串转十六进制字符串
     *
     * @param str
     * @return
     */
    public static String str2HexString(String str) {
        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder();
        byte[] bs = null;
        try {
            bs = str.getBytes("gbk");
        } catch (Exception e) {
            e.printStackTrace();
        }
        int bit;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
        }
        return sb.toString();
    }

    /**
     * 把十六进制字符串转换成十六进制字节数组
     *
     * @param
     * @return byte[]
     */
    public static byte[] hexStringToHexBytes(String hex) {
        int len = (hex.length() / 2);
        byte[] result = new byte[len];
        char[] achar = hex.toCharArray();
        for (int i = 0; i < len; i++) {
            int pos = i * 2;
            result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
        }
        return result;
    }

    private static int toByte(char c) {
        byte b = (byte) "0123456789ABCDEF".indexOf(c);
        return b;
    }

    /**
     * 十六进制把字符串转换成十六进制字节数组
     *
     * @param src
     * @return
     */
    public static byte[] hexString2Bytes(String src) {
        if (null == src || 0 == src.length()) {
            return null;
        }
        byte[] ret = new byte[src.length() / 2];
        byte[] tmp = src.getBytes();
        for (int i = 0; i < (tmp.length / 2); i++) {
            ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
        }
        return ret;
    }

    public static byte uniteBytes(byte src0, byte src1) {
        byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue();
        _b0 = (byte) (_b0 << 4);
        byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue();
        byte ret = (byte) (_b0 ^ _b1);
        return ret;
    }

    /**
     * 十六进制把字符串转换成十六进制字节数组
     *
     * @param bArray
     * @return
     */
    public final static String bytesToHexString(byte[] bArray) {
        StringBuffer sb = new StringBuffer(bArray.length);
        String sTemp;
        for (int i = 0; i < bArray.length; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2)
                sb.append(0);
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }

    /**
     * 十六进制字符串转十进制字符串
     * @param hex
     * @return
     */
    public static String hex2String(String hex) {
        return bytes2String(hexString2Bytes(hex));
    }

    /**
     *  字节数组转字符串
     * @param arr
     * @return
     */
    public static String bytes2String(byte[] arr) {
        try {
            return new String(arr, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static boolean isInteger(String value) {
        try {
            Integer.parseInt(value);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    /**
     * 判断字符串是否是浮点数
     */
    public static boolean isDouble(String value) {
        try {
            Double.parseDouble(value);
            if (value.contains("."))
                return true;
            return false;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    /**
     * 判断字符串是否是数字
     */
    public static boolean isNumber(String value) {
        return isInteger(value) || isDouble(value);
    }


    //验证字符串中包含数字，很简单。
    public void isContainNumber(String str) {
        boolean isNumber = true;
        char[] ch = str.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            isNumber = Character.isDigit(ch[i]);
            if (isNumber)
                break;
        }
        if (isNumber)
            System.out.println("输入的字符串中包含数字!");
    }

    /*
      * 判断是否为整数
      * @param str 传入的字符串
      * @return 是整数返回true,否则返回false
    */
    public static boolean iSInteger(String str) {
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        return pattern.matcher(str).matches();
    }

    /**
     * 判断当前字符是否是整数或者是带一位小数的浮点数
     * @param str
     * @return
     */
    public static boolean isIntegerOrFloat(String str) {
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]+(\\.\\d)?$");
        return pattern.matcher(str).matches();
    }

    /**
     * 拼接字符串
     * @param arg
     * @param args
     * @return  拼接完成的字符串
     */
    public static String join(String arg, String... args) {
        StringBuffer buffer = new StringBuffer(arg);
        for(String s : args) {
            buffer.append(s);
        }
        return  buffer.toString();
    }

}
